package com.yycx.module.bbs.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bbs.client.entity.BbsType;
import com.yycx.module.bbs.provider.mapper.BbsTypeMapper;
import com.yycx.module.bbs.provider.service.BbsTypeService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 帖子分类 接口实现类
 *
 * @author flyme
 * @date 2019-12-06
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BbsTypeServiceImpl extends BaseServiceImpl<BbsTypeMapper, BbsType> implements BbsTypeService {

    @Override
    public ResultBody baseAdd(CriteriaSave cs, BbsType obj) {
        obj.setGroupId(CommonConstants.INT_1);
        return super.baseAdd(cs, obj);
    }

    @Override
    public List<EntityMap> listByGroupId(Long groupId) {
        CriteriaQuery<BbsType> cq = new CriteriaQuery(BbsType.class);
        cq.eq("groupId", groupId);
        cq.eq("typeState", CommonConstants.ENABLED);
        cq.orderByAsc("sortOrder");
        return selectEntityMap(cq);
    }

    @Override
    public List<EntityMap> listTypeTypeId(String typeId) {
        CriteriaQuery<BbsType> cq = new CriteriaQuery(BbsType.class);
        ApiAssert.isNotEmpty("typeId不能为空", typeId);
        cq.select(BbsType.class, "typeId", "typeName");
        String[] typeIds = typeId.split(",");
        cq.lambda().eq(BbsType::getTypeState, CommonConstants.ENABLED).orderByAsc(BbsType::getSortOrder);
        if (typeIds.length > 1) {
            cq.regexp("typeId", typeId);
        } else {
            cq.eq("typeId", typeId);
        }
        return selectEntityMap(cq);
    }
    @Override
    public List<String> selectTagNames(String typeIds) {
        List<String> tagNames = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(typeIds)) {
            CriteriaQuery qw = new CriteriaQuery(BbsType.class);
            qw.select("typeName");
            qw.in(true, "typeId", typeIds.split(","));
            tagNames = listObjs(qw,e -> e.toString());
        }
        return tagNames;
    }

}
